/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package config;

import java.util.Hashtable;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchResult;

/**
 *
 * @author amega
 */
public class LdapAuth {
    
    
    public boolean  checkUser(String login, String passwrd) {
        String username = login;
        String password = passwrd;
        String base = "ou=people,dc=example,dc=com";
        String dn = "uid=" + username + "," + base;
        String ldapURL = "ldap://localhost:389";

        Hashtable<String,String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, ldapURL);
        env.put(Context.SECURITY_AUTHENTICATION,"simple");
        env.put(Context.SECURITY_PRINCIPAL,dn);
        env.put(Context.SECURITY_CREDENTIALS, password);
        
        try {
            DirContext au = new InitialDirContext(env);
            return true;
            //System.out.print("!!!!YEs"+au.getAttributes("uid=amega"));
        } catch (AuthenticationException  e) {
            System.out.print("!!Eroor: "+e.getMessage());
        } catch (NamingException e) {
            System.out.print("!!Error: "+e.getMessage());
        }
        
        return false;
    }
    
}
